package com.gdit.zax.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.DynamicDao;
import com.gdit.zax.dao.UserDao;
import com.gdit.zax.entitys.CrouseTree;
import com.gdit.zax.entitys.UserInfo;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.O;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.VeriFicUtil;

/**
 * 获取用户学习树
 * @author lenovo
 *
 */
public class JUserTree extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	//http://pingkgs3pc:8080/zuiaixue/usertree?accesstoken=97d595542939f84b884cf93fc9b543c390e436342d4cb9c1
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String accesstoken = request.getParameter("accesstoken");
		int page = TextUtil.String2int(request.getParameter("page"), 1);
		int uid = VeriFicUtil.getVFID(accesstoken);
		Connection conn = DBUtil.getConnection();
		
		if (uid==Integer.MIN_VALUE || uid == Integer.MAX_VALUE) {
			O.printNoLogin(response);
			return;
		}
		
		if (conn==null) {
			O.printFail(response, 1000, "数据库连接失败!");
			return;
		}
		
		DAOFactory dao  = DAOFactory.init(conn, request, response);
		DynamicDao ddap = dao.getDynamicDao();
		
		ArrayList<CrouseTree> list = ddap.getUserLearnTree(uid, page, 1000);
		ArrayList<HashMap<String, Object>> list2 = new ArrayList<HashMap<String,Object>>();
		for (int i = 0; i < list.size(); i++) {
			CrouseTree tree = list.get(i);
			HashMap<String, Object> map = new HashMap<String, Object>();	
			map.put("time", tree.getT_seerecord_addtime());
			map.put("voideid", tree.getT_video_id());
			map.put("pic", Constant.WEBRoot +tree.getT_video_pic());
			map.put("content", "学完课程：" + tree.getT_course_name());
			map.put("voidename", tree.getT_video_name());
			list2.add(map);
		}
		
		O.printSuccess(response, list2);
	}
	

}
